package com.sy.module_clockin

import android.content.Intent
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModelFragment2
import com.fenghuajueli.module_route.ClockInModuleRoute
import com.sy.module_clockin.annotation.ClockInType
import com.sy.module_clockin.databinding.ModuleClockinFragmentHomeBinding
import com.sy.module_clockin.databinding.ModuleClockinItemFunclistBinding
import com.sy.module_clockin.dialog.ClockInDialog
import com.sy.module_clockin.model.ClockInModel
import com.sy.module_clockin.recycler.bindAdapter
import com.sy.module_clockin.recycler.setOnFastClickListener
import com.sy.module_clockin.utils.TimeUtils
import java.text.SimpleDateFormat
import java.util.*


@Route(path = ClockInModuleRoute.CLOCKIN_PAGE)
class ClockinFragment : BaseViewModelFragment2<ClockInModel, ModuleClockinFragmentHomeBinding>() {
    private val TAG = "ClockinFragment"

    /**
     * 打卡类型
     */
    @ClockInType
    private var clockInType: Int = ClockInType.CLOCKIN_TYPE_GET_UP_EARLY

    private val format by lazy { SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault()) }


    override fun createViewModel(): ClockInModel {
        return ViewModelProvider(this, defaultViewModelProviderFactory).get(ClockInModel::class.java)
    }

    override fun createViewBinding(): ModuleClockinFragmentHomeBinding {
        return ModuleClockinFragmentHomeBinding.inflate(layoutInflater, null, false)
    }

    override fun initView(containerView: View?) {
        binding.btnBack.setOnFastClickListener {
            requireActivity().finish()
        }

        initFuncList()

        binding.tvClockInRecord.setOnFastClickListener {
            startActivity(Intent(requireContext(), ClockinRecordActivity::class.java))
        }

        binding.btnClockIn.setOnFastClickListener {
            ClockInDialog(clockInType, model).show(this.childFragmentManager, TAG)
        }

        model.timeData.observe(this, {
            it?.apply {
                val instance = Calendar.getInstance()
                binding.tvClockInTime.text = format.format(instance.time)
                binding.clockinView.setClockInTime(instance[Calendar.HOUR_OF_DAY])
            }
        })

        model.startTime()

        init()

    }

    private fun init() {
        model.cancelTime()
        binding.btnClockIn.isEnabled = true

        when (clockInType) {
            ClockInType.CLOCKIN_TYPE_GET_UP_EARLY -> {
                binding.clockinView.setProgressColor(intArrayOf(0XFF91D425.toInt(), 0XFF17A83E.toInt()))
                binding.tvClockInTitle.setTextColor(0XFF91D425.toInt())
                binding.tvClockInTime.setTextColor(0XFF8E99AF.toInt())
                binding.tvClockInTip.text = getString(R.string.module_clockin_getup_tip)
                binding.clClockContainer.setBackgroundResource(R.mipmap.module_clockin_round_02_blue)
                binding.btnClockIn.setBackgroundResource(R.drawable.module_clockin_round_blue)
            }

            ClockInType.CLOCKIN_TYPE_MOVEMENT -> {
                binding.clockinView.setProgressColor(intArrayOf(0XFFFBBD78.toInt(), 0XFFE4743D.toInt()))
                binding.tvClockInTitle.setTextColor(0XFFE78353.toInt())
                binding.tvClockInTime.setTextColor(0XFFAF9F8E.toInt())
                binding.tvClockInTip.text = getString(R.string.module_clockin_movement_tip)
                binding.clClockContainer.setBackgroundResource(R.mipmap.module_clockin_round_02_yellow)
                binding.btnClockIn.setBackgroundResource(R.drawable.module_clockin_round_yellow)
            }

            ClockInType.CLOCKIN_TYPE_DRINK_WATER -> {
                binding.clockinView.setProgressColor(intArrayOf(0XFF7AFCFA.toInt(), 0XFF39BAD7.toInt()))
                binding.tvClockInTitle.setTextColor(0XFF39BBD8.toInt())
                binding.tvClockInTime.setTextColor(0XFF8EADAF.toInt())
                binding.tvClockInTip.text = getString(R.string.module_clockin_drink_tip)
                binding.clClockContainer.setBackgroundResource(R.mipmap.module_clockin_round_02_cyan)
                binding.btnClockIn.setBackgroundResource(R.drawable.module_clockin_round_cyan)
            }

            ClockInType.CLOCKIN_TYPE_READING -> {
                binding.clockinView.setProgressColor(intArrayOf(0XFFFEACAD.toInt(), 0XFFDF5858.toInt()))
                binding.tvClockInTitle.setTextColor(0XFFDE5858.toInt())
                binding.tvClockInTime.setTextColor(0XFFAF8E8E.toInt())
                binding.tvClockInTip.text = getString(R.string.module_clockin_reading_tip)
                binding.clClockContainer.setBackgroundResource(R.mipmap.module_clockin_round_02_pink)
                binding.btnClockIn.setBackgroundResource(R.drawable.module_clockin_round_pink)
            }
        }


        val (startTime, endTime) = TimeUtils.getTodayStartEnd()
        model.getClockInByTimeRangeAndType(requireContext(), clockInType, startTime, endTime).observe(this, {
            if (!it.isNullOrEmpty() && clockInType == ClockInType.CLOCKIN_TYPE_GET_UP_EARLY) {
                binding.btnClockIn.setBackgroundResource(R.mipmap.module_clockin_btn_clockin_after)
                binding.btnClockIn.isEnabled = false
                val instance = Calendar.getInstance()
                instance.timeInMillis = it[0].recordTime
                binding.tvClockInTime.text = format.format(instance.time)
                binding.clockinView.setClockInTime(instance[Calendar.HOUR_OF_DAY])
            } else {
                model.startTime()
            }
        })
    }

    private fun initFuncList() {
        binding.rclFuncList.bindAdapter<Pair<String, Int>, ModuleClockinItemFunclistBinding> {
            layoutManger = LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
            dataList = arrayListOf(
                getString(R.string.module_clockin_early) to R.drawable.module_clockin_icon_early_selector,
                getString(R.string.module_clockin_movement) to R.drawable.module_clockin_icon_movement_selector,
                getString(R.string.module_clockin_drinking) to R.drawable.module_clockin_icon_drinking_selector,
                getString(R.string.module_clockin_reading) to R.drawable.module_clockin_icon_reading_selector,
            )

            onBindView { itemViewHolder, itemData, position ->
                itemViewHolder.tvName.text = itemData.first
                itemViewHolder.tvName.setCompoundDrawablesWithIntrinsicBounds(0, itemData.second, 0, 0)
                itemViewHolder.tvName.isSelected = clockInType == position
            }

            onItemClick { itemViewHolder, itemData, position ->
                val temp = clockInType
                when (position) {
                    0 -> clockInType = ClockInType.CLOCKIN_TYPE_GET_UP_EARLY

                    1 -> clockInType = ClockInType.CLOCKIN_TYPE_MOVEMENT

                    2 -> clockInType = ClockInType.CLOCKIN_TYPE_DRINK_WATER

                    3 -> clockInType = ClockInType.CLOCKIN_TYPE_READING
                }

                binding.rclFuncList.adapter?.notifyItemChanged(temp)
                binding.rclFuncList.adapter?.notifyItemChanged(position)
                init()

            }
        }
    }

    override fun initData() {

    }

}